<?php
/**
 * 框架助手过滤器
 * 
 * 用来实现在每个控制器页面显示一个Toolbar，以方便操作
 * 
 * 要想关掉框架助手，可以在IApplication::start()之前调用：
 * <code>
 *  define("IF_HELPER", "off");
 * </code>
 * 或在控制器中定义一个属性：
 * class MyController {
 * 	private $_helper = false;
 * }
 *
 * @package if
 * @subpackage module.helper
 * @since 1.0
 */
class IHelperFilter extends IActionFilter {
	private $isStarted = false;
	
	/**
	 * Enter description here...
	 *
	 * @param IController $controller
	 */
	function _before(&$controller) {
		parent::_before($controller);
		if (defined("IF_HELPER") && strtolower(IF_HELPER) == "off") {
			return;
		}
		
		$path = $controller->request()->uriObj()->path();
		$ext = strtolower(pathinfo($path, PATHINFO_EXTENSION));
		if (!isset($attrs["_helper"]) && $ext && !in_array($ext, array("html", "htm", "do", "php", "phtml", "action", "py", o("@rouer.ext", "rb", "rhtml")))) {
			return;
		}
		
		$attrs = $controller->attrs();
		if (isset($attrs["_helper"]) && !$attrs["_helper"]) {
			return;
		}
		
		$this->isStarted = true;
		ob_start();
	}
	
	/**
	 * Enter description here...
	 *
	 * @param IController $controller
	 */
	function _after(&$controller) {
		if (!$this->isStarted) {
			return;
		}
		parent::_after($controller);
		if (defined("IF_HELPER") && strtolower(IF_HELPER) == "off") {
			return;
		}		
		
		$content = ob_get_clean();
		echo $this->_doFilter($content);
	}
	
	private function _doFilter($content) {
		$request = $this->controller()->request();
		if ($request->isCli() || $request->isAjax() || $request->isPut() || $request->isFlash() || $request->isDelete()) {
			return $content;
		}
		if (preg_match("/<\/body>/i", $content)) {
			$content = preg_replace("/<\/body>/i", "\n" . $this->_renderHelper($content) . "\n</body>", $content);
		}
		else {
			$content = "<html>\n<body>\n" . $content . "\n" . $this->_renderHelper($content) . "\n</body>\n</html>";
		}
		return $content;
	}
	
	private function _renderHelper($content) {
		//当前控制器所有动作
		$methods = get_class_methods($this->controller());
		$actions = array();
		if ($this->controller()->router()) {
			foreach ($methods as $method) {
				if (strlen($method) > 2 && strtolower(substr($method, 0, 2)) == "do") {
					$action = substr($method, 2);
					$action{0} = strtolower($action{0});
					$path = $this->controller()->path($action);
					if (strcasecmp($method, $this->controller()->action()->method()) == 0) {
						$action = "<strong>" . $action . "</strong>";
					}
					$actions[] = "<a href=\"{$path}\">{$action}</a>";
				}
			}
		}
		
		//TPL
		$view = new ISmartyView();
		$view->setController($this->controller());
		$viewDirectory = $this->controller()->viewDirectory();
		if ($viewDirectory) {
			$tpl = $viewDirectory . "/" . $view->tpl(o("@view.smarty.ext"));
		}
		else {
			$tpl = o("@view.smarty.template_dir") . "/" . $view->tpl(o("@view.smarty.ext"));
		}
		$tpl = urlencode(str_replace("%{locale}", ILocale::current()->name(), $tpl));
				
		//RENDER
		$jsDir = o("@ui.javascript.basedir");
		$render = "\n<!-- render helper script begin -->\n";
		$scriptLibs = UIControl::scriptsLibs();
		if (!in_array("if", $scriptLibs)) {
			$render .= '<script type="text/javascript" src="' . __BASE__ . '/' . $jsDir . '/if.js"></script>' . "\n";
		}
		if (!in_array("jquery", $scriptLibs) && !preg_match("/<[^>]+jquery([\w-\.]+)?\.js[^<]+>/i", $content)) {
			$render .= '<script type="text/javascript" src="' . __BASE__ . '/' . $jsDir . '/jquery.js"></script>' . "\n";
		}
		$render .= '<script type="text/javascript" src="' . __BASE__ . '/' . $jsDir . '/helper.js"></script>' . "\n";
		$render .= '<style type="text/css">
#if_helper_body { font-size:12px; line-height:1.4; z-index:999999; }
#if_helper_body .selected { background:#eee }
</style>
<script language="javascript">
var ifHelperRouter = "' . __ROUTER__ . '";
var ifHelper;
var ifHelperTpl = "' . $tpl . '";
var ifHelperController = "' . __CONTROLLER__ . '";
var ifHelperAction = "' . __ACTION__ . '";
var ifHelperControllerFile = "' . urlencode($this->controller()->file(true)) . '";
$(function () {
	ifHelper = new IF_Helper();
	ifHelper.init();
});
</script>
<!-- render helper script end -->';
		return $render;
	}
}

?>